import { defineStore } from 'pinia';
import { ref } from 'vue';

interface UserInfoState {
	userId: string | number | undefined;
	userName: string;
	// 用户身份
	userType: 'SOLVER' | 'SEEKER' | '';
	membershipStatus: 'GENERAL' | 'VIP' | '' ;
}

// 定义 Store
export const useUserStore = defineStore('user', () => {
	// token信息
	const token = ref<string>('');

	// 保存token，登录时使用
	const setToken = (val: string) => {
		token.value = val;
	};

	// 清理token，退出时使用
	const clearToken = () => {
		token.value = '';
	};
	// 用户信息
	const userInfo = ref<UserInfoState>({
		userId: '',
		userName: '',
		userType: '',
		membershipStatus: ''
	});

	// 保存用户信息，登录时使用
	const setUserInfo = (val: Partial<UserInfoState>) => {
		userInfo.value = {
			...userInfo.value,
			...val
		};
	};

	// 清理用户信息，退出时使用
	const clearUserInfo = () => {
		userInfo.value = {
			userId: '',
			userName: '',
			userType: '',
			membershipStatus: ''
		};
	};

	return {
		token,
		setToken,
		clearToken,
		userInfo,
		setUserInfo,
		clearUserInfo
	};
});
